import plugin from '../../lib/plugins/plugin.js'
import YAML from "yaml"
import fs from "fs"

let file = 'data/pixiv.yaml';
if (!fs.existsSync(file)) {
    fs.writeFileSync(file, `{}`)
}
export class jinyong extends plugin {
    constructor() {
        super({
            name: "涩涩开关",
            dsc: "控制机器人在指定群开关",
            event: "message",
            priority: 1000, //指令优先级
            rule: [
			{
                reg: '^#(开|关)(涩涩|色色|瑟瑟|st|setu)$', 
                fnc: "setuon",
            },
            {
                reg: '^#(开|关)18$',
                fnc: "r18",
            },
			{
                reg: '^#?(cd|CD|冷却)时间设置(.*)$',
                fnc: "setucd",
            },
			{
                reg: '^#?撤回时间设置(.*)$',
                fnc: "setuback",
            },
			{
                reg: '^#?查看(涩图|好看的|st|setu|瑟图|色图|瑟瑟)设置$',
                fnc: "setu",
            },
           ]
        });
    }
 
  async setu(e) {
    let data = YAML.parse(fs.readFileSync(file, 'utf8'))
	if (data[e.group_id]) {
        e.reply(data[e.group_id],false,{ recallMsg: 20 })
	}
    return true			
  }

  async setuon(e) {
    if (!e.isGroup) return
	if (!e.isMaster && !e.member.is_owner && !e.member.is_admin) return

    let id = e.group_id
    let data = await loadfile(id)
	if (/#开(涩涩|色色|瑟瑟|st|setu)/.test(e.msg)) {
        data[id].setu = true
        await fs.writeFileSync(file, YAML.stringify(data));
        e.reply(`[${e.group_id}]涩图功能已开启～`,false,{ recallMsg: 10 });
    } else if (/#关(涩涩|色色|瑟瑟|st|setu)/.test(e.msg)) {
		data[id].setu = false
        await fs.writeFileSync(file, YAML.stringify(data));
        e.reply(`[${e.group_id}]涩图功能已关闭～`,false,{ recallMsg: 10 });
    }					
	return true			
  }

  async r18(e) {
    if (!e.isGroup || !e.isMaster) return

    let id = e.group_id
    let data = await loadfile(id)
	if (e.msg == '#开18') {
        data[id].r18 = true
        await fs.writeFileSync(file, YAML.stringify(data));
        e.reply(`[${e.group_id}]涩图18已开启～`,false,{ recallMsg: 10 });
	} else if (e.msg == '#关18') {
	    data[id].r18 = false
        await fs.writeFileSync(file, YAML.stringify(data));
        e.reply(`[${e.group_id}]涩图18已关闭～`,false,{ recallMsg: 10 });
	}
	return true			
  }

  async setucd(e) {
    if (!e.isGroup || !e.isMaster) return
	let id = e.group_id
    let data = await loadfile(id)

	let key = e.msg.replace(/#?(cd|CD|冷却)时间设置/g, '').trim();
    data[id].cd = Number(key);
    await fs.writeFileSync(file, YAML.stringify(data));
	e.reply(`[${e.group_id}]涩图cd已设置 ${key}秒～`,false,{ recallMsg: 10 })
	return true
  }
  
  async setuback(e) {
	if (!e.isGroup || !e.isMaster) return
	let id = e.group_id
    let data = await loadfile(id)

	let key = e.msg.replace(/#?撤回时间设置/g, '').trim();
    data.recall = Number(key);
    await fs.writeFileSync(file, YAML.stringify(data));
	e.reply(`[${e.group_id}]涩图撤回已设置 ${key}秒～`,false,{ recallMsg: 10 })
	return true
  }
	
}

async function loadfile(id) {
	let data = YAML.parse(fs.readFileSync(file, 'utf8'))
	if (!data[id]) {
	    data[id] = { setu: false, r18: false, cd: 30, recall: 120 }
		logger.mark(`${id}自动创建权限文件`)
	}
	return data
}